package com.doit.day04

import scala.collection.immutable
import scala.io.Source

object _02_线段案例 {
  def main(args: Array[String]): Unit = {
    val list: List[String] = Source.fromFile("data/line.txt").getLines().toList    //list("1,2","2,3","3,6"....)
    val list1: List[immutable.IndexedSeq[String]] = list.map(line => {
      val arr: Array[String] = line.split(",")
      val start: Int = arr(0).toInt
      val end: Int = arr(1).toInt
      for (elem <- start until end) yield {
        elem + "-" + (elem + 1)
      }
    })
    //下面得结果怎么得到//list(list("1-2","1-2"),list("1-2","2-3","2-3")...)
    //分组之前得到得结果 list("1-2","1-2","1-2","2-3","2-3"...)
    val flattened: List[String] = list1.flatten
    //分组
    flattened.groupBy(word=>word)
      .map(tp=>{
        //"1-2"
        (tp._1,tp._2.size)
      })
      .toList
      .sortBy(_._2)
      .foreach(println)
    //最终要的是什么 ==> map("1-2",3   "2-3",5)  list(("1-2",3),("1-2",3))
    //wordCount


    val arr: Array[Int] = Array(1, 2, 3, 4)
    val ints: Array[Int] = arr.takeRight(2)
    println(ints.mkString(","))


  }
}
